//
//  DXTextView+Extension.swift
//  weiBoOfSwift
//
//  Created by simon on 15/12/18.
//  Copyright © 2015年 simon. All rights reserved.
//

import UIKit

extension DXTextView{

    var statuesText:String{
    
        get{
            var statuesTextString:String = String()
            
            attributedText.enumerateAttributesInRange(NSRange.init(location: 0, length: attributedText.length), options: [], usingBlock: { (dict, ranger, _) -> Void in

                if let attachment = dict["NSAttachment"] {
                    let emotionChs = (attachment as! DXEmotionAttachment).emotionModel.chs
                    
                    statuesTextString += emotionChs!
                    //           logPrint("*******\(emotionCht)")
                    
                }else{
                    let text = (self.attributedText.string as NSString).substringWithRange(ranger)
                    statuesTextString += text
                    //            logPrint("******\(text)")
                    
                }
            })
            
//            logPrint("******\(statuesTextString)")
        return statuesTextString
        }
    }
    
    func insertAttributeString(attString:NSAttributedString){
        
        let originMutableAttri =  NSMutableAttributedString(attributedString: attributedText)
        
        //            生成图片附件
//        let imgAttribute = DXEmotionAttachment(emotion: emotion)
//        let imgAttibutM = imgAttribute.getAttributString(font!)
        var range = selectedRange
        //            将生成的文本插入到textview中富文本中去
        originMutableAttri.replaceCharactersInRange(range, withAttributedString: attString)
        attributedText = originMutableAttri
        
        range.location += 1
        
        range.length = 0
        
        selectedRange = range
        NSNotificationCenter.defaultCenter().postNotificationName(UITextViewTextDidChangeNotification, object: nil)
        delegate?.textViewDidChange!(self)
    }


}